<!DOCTYPE html>
<html layout:decorate="~{layout}" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">

<head>
    <meta content="IE=edge" http-equiv="X-UA-Compatible"/>
    <meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name="viewport"/>

    <title th:text="#{cas.mfa.u2f.pagetitle}">U2f Login View</title>
    <link href="../../static/css/cas.css" rel="stylesheet" th:remove="tag"/>
    <script th:src="@{/js/u2f/u2f-api.js}"></script>

    <script th:inline="javascript">

        /*<![CDATA[*/

        var appId =  /*[[${u2fAuth.appId}]]*/;

        var version =  /*[[${u2fAuth.version}]]*/;

        var challenge =  /*[[${u2fAuth.challenge}]]*/;

        var keyHandle =  /*[[${u2fAuth.keyHandle}]]*/;

        setTimeout(function () {
            var registeredKeys = [{version: version, keyHandle: keyHandle}];

            u2f.sign(appId, challenge, registeredKeys, function (data) {
                var form = document.getElementById('form');
                var reg = document.getElementById('tokenResponse');
                reg.value = JSON.stringify(data);
                form.submit();
            });
        }, 1000);

        /*]]>*/

    </script>

</head>

<body>
<main class="container mt-3 mb-3" role="main">
    <div class="mdc-card mdc-card-content w-lg-50 p-4 m-auto" id="login" layout:fragment="content">
        <h3 th:text="#{cas.mfa.u2f.authentication.device}">Authenticate Device</h3>
        <div th:remove="tag" th:utext="#{cas.mfa.u2f.authentication.message}">
            <p><strong>Please touch the flashing U2F device now.</strong></p>
            <p> You may be prompted to allow the site permission to access your security keys. After granting
                permission, the device will start to blink.</p>
        </div>

        <form id="form" method="POST" th:action="@{/login}" th:object="${credential}">
            <input id="tokenResponse" name="tokenResponse" th:field="*{token}" type="hidden"/>
            <input name="_eventId" type="hidden" value="submit"/>
            <input name="execution" th:value="${flowExecutionKey}" type="hidden"/>
            <input name="geolocation" type="hidden"/>
        </form>
    </div>
</main>
</body>

</html>